using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace TitleBarColorTest
{
    // Windows API 声明用于设置标题栏颜色
    internal class NativeMethods
    {
        [DllImport("dwmapi.dll")]
        public static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, ref int attrValue, int attrSize);
        
        public const int DWMWA_CAPTION_COLOR = 35;  // Windows 10+ (Build 18985+)
        public const int DWMWA_TEXT_COLOR = 36;     // Windows 10+ (Build 18985+)
    }
    
    /// <summary>
    /// 简化版主题管理器类，用于测试标题栏颜色功能
    /// </summary>
    public class ThemeManager
    {
        /// <summary>
        /// 检查当前Windows版本是否支持DWMWA_CAPTION_COLOR
        /// </summary>
        /// <returns>如果支持返回true，否则返回false</returns>
        private bool IsCaptionColorSupported()
        {
            try
            {
                var osVersion = Environment.OSVersion.Version;
                // DWMWA_CAPTION_COLOR 需要 Windows 10 Build 18985 或更高版本
                bool supported = osVersion.Build >= 18985;
                System.Diagnostics.Debug.WriteLine($"OS Version: {osVersion}, Build >= 18985: {supported}");
                Console.WriteLine($"OS Version: {osVersion}, Build >= 18985: {supported}");
                return supported;
            }
            catch (Exception ex)
            {
                // 如果无法确定版本，默认返回true并在实际调用时处理错误
                System.Diagnostics.Debug.WriteLine($"Error checking OS version: {ex.Message}");
                Console.WriteLine($"Error checking OS version: {ex.Message}");
                return true;
            }
        }

        /// <summary>
        /// 应用标题栏颜色
        /// </summary>
        /// <param name="form">目标窗体</param>
        public void ApplyTheme(Form form)
        {
            // 检查当前系统是否支持标题栏颜色设置
            bool isSupported = IsCaptionColorSupported();
            System.Diagnostics.Debug.WriteLine($"IsCaptionColorSupported: {isSupported}");
            Console.WriteLine($"IsCaptionColorSupported: {isSupported}");
            
            if (!isSupported)
            {
                System.Diagnostics.Debug.WriteLine("当前Windows版本不支持标题栏颜色设置");
                Console.WriteLine("当前Windows版本不支持标题栏颜色设置");
                return;
            }

            try
            {
                // 设置标题栏背景色为蓝色
                Color titleBarColor = Color.FromArgb(33, 150, 243); // #2196F3
                int captionColorValue = (titleBarColor.B << 16) | (titleBarColor.G << 8) | titleBarColor.R | (titleBarColor.A << 24);
                System.Diagnostics.Debug.WriteLine($"Caption color value: {captionColorValue:X8}");
                Console.WriteLine($"Caption color value: {captionColorValue:X8}");
                
                int result = NativeMethods.DwmSetWindowAttribute(form.Handle, NativeMethods.DWMWA_CAPTION_COLOR, ref captionColorValue, sizeof(int));
                System.Diagnostics.Debug.WriteLine($"DwmSetWindowAttribute (Caption Color) result: {result}");
                Console.WriteLine($"DwmSetWindowAttribute (Caption Color) result: {result}");
                
                // 检查API调用结果
                if (result != 0)
                {
                    System.Diagnostics.Debug.WriteLine($"DwmSetWindowAttribute failed with code: {result}");
                    Console.WriteLine($"DwmSetWindowAttribute failed with code: {result}");
                }
                
                // 设置标题栏文字色为白色
                Color titleBarTextColor = Color.White;
                int textColorValue = (titleBarTextColor.B << 16) | (titleBarTextColor.G << 8) | titleBarTextColor.R | (titleBarTextColor.A << 24);
                System.Diagnostics.Debug.WriteLine($"Text color value: {textColorValue:X8}");
                Console.WriteLine($"Text color value: {textColorValue:X8}");
                
                result = NativeMethods.DwmSetWindowAttribute(form.Handle, NativeMethods.DWMWA_TEXT_COLOR, ref textColorValue, sizeof(int));
                System.Diagnostics.Debug.WriteLine($"DwmSetWindowAttribute (Text Color) result: {result}");
                Console.WriteLine($"DwmSetWindowAttribute (Text Color) result: {result}");
                
                // 检查API调用结果
                if (result != 0)
                {
                    System.Diagnostics.Debug.WriteLine($"DwmSetWindowAttribute failed with code: {result}");
                    Console.WriteLine($"DwmSetWindowAttribute failed with code: {result}");
                }
                
                // 刷新窗体以使更改生效
                System.Diagnostics.Debug.WriteLine("Applying refresh methods to make title bar color changes visible");
                Console.WriteLine("Applying refresh methods to make title bar color changes visible");
                form.Invalidate();
                form.Update();
                System.Diagnostics.Debug.WriteLine("Refresh methods applied");
                Console.WriteLine("Refresh methods applied");
            }
            catch (Exception ex)
            {
                // 忽略标题栏颜色设置的错误，因为这可能在某些Windows版本上不支持
                System.Diagnostics.Debug.WriteLine($"设置标题栏颜色时出错: {ex.Message}");
                System.Diagnostics.Debug.WriteLine($"Exception stack trace: {ex.StackTrace}");
                Console.WriteLine($"设置标题栏颜色时出错: {ex.Message}");
                Console.WriteLine($"Exception stack trace: {ex.StackTrace}");
            }
        }
    }
}